package com.sky.service.impl;

import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.ReportService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    /**
     * 获取今日运营数据
     * @return
     */
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public BusinessDataVO getBusinessData() {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(LocalDate.now());
        //获取今日新增用户数
        List<Integer> newUsersByDates = reportMapper.getNewUsersByDates(dateList);
        Integer newUsers = newUsersByDates.get(0);

        //获取今日有效订单数
        List<Integer> validOrderCountByDates = reportMapper.getValidOrderCountByDates(dateList);
        Integer validOrderCount = validOrderCountByDates.get(0);
        //获取今日营业额
        List<BigDecimal> amountsByDates = reportMapper.getAmountsByDates(dateList);
        BigDecimal turnover = amountsByDates.get(0);

        //获取今日订单总数
        List<Integer> orderCountByDates = reportMapper.getOrderCountByDates(dateList);
        Integer orderCount = orderCountByDates.get(0);

        //计算今日平均客单价
        List<Integer> totalUsersByDates = reportMapper.getTotalUsersByDates(dateList);
        Integer totalUsers = totalUsersByDates.get(0);
        // 通过将总营业额除以总用户数，并四舍五入到小数点后两位
        Double unitPrice = turnover.divide(new BigDecimal(totalUsers), 2, RoundingMode.HALF_UP).doubleValue();
        //计算今日订单完成率
        Double orderCompletionRate = validOrderCount / orderCount.doubleValue();
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover.doubleValue())
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 获取订单统计数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(LocalDate.now());
        //获取今日订单总数
        List<Integer> orderCountByDates = reportMapper.getOrderCountByDates(dateList);
        Integer allOrders = orderCountByDates.get(0);

        //获取今日已取消的订单数
        List<Integer> cancelledOrdersByDates = reportMapper.getCancelledOrdersByDates(dateList);
        Integer cancelledOrders = cancelledOrdersByDates.get(0);
        //获取今日已完成的订单数
        List<Integer> completeByDates = reportMapper.getValidOrderCountByDates(dateList);
        Integer completedOrders = completeByDates.get(0);
        //获取今日待派送的订单数
        List<Integer> deliverOrdersByDates = reportMapper.getDeliverOrdersByDates(dateList);
        Integer deliverOrders = deliverOrdersByDates.get(0);
        //获取今日待接单的订单数
        List<Integer> waitingOrdersByDates = reportMapper.getWaitingOrdersByDates(dateList);
        Integer  waitingOrders = waitingOrdersByDates.get(0);



        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliverOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
    /**
     * 获取菜品统计数据
     * @return
     */

    @Override
    public DishOverViewVO getDishOverView() {
        //获取已停售的菜品数量
        Integer discontinued = dishMapper.countByStatus(0);
        //获取起售中的菜品数量
        Integer sold = dishMapper.countByStatus(1);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //获取已停售的菜品数量
        Integer discontinued = setmealMapper.countByStatus(0);
        //获取起售中的菜品数量
        Integer sold = setmealMapper.countByStatus(1);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
